﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using smp.QueryDb.Structure;

namespace smp.QueryDb.Executing
{
    public class ExecuteRead : Execute
    {
        private readonly List<string> _fields = new List<string>();

        public ExecuteRead(Procedure procedure)
            : base(procedure)
        {
        }

        internal virtual List<string> Fields { get { return _fields; } }

        public ExecuteRead SetOutput(string fieldName)
        {
            if (_fields.Contains(fieldName))
            {
                throw Helper.Error("Cannot add duplicate output field {0} to ExecuteRead for Procedure {1}",
                                   fieldName,
                                   Procedure.Declaration);
            }
            _fields.Add(fieldName);
            return this;
        }
    }

    public class ExecuteRead<TViewModel> : ExecuteRead
    {
        private readonly Dictionary<string, PropertyInfo> _properties = new Dictionary<string, PropertyInfo>();

        public ExecuteRead(Procedure procedure)
            : base(procedure)
        {
        }

        internal override List<string> Fields
        {
            get
            {
                return _properties.Select(item => item.Key).ToList();
            }
        }

        public IExecuteReadTo<TViewModel> Map(Expression<Func<TViewModel, object>> propertyExpression)
        {
            var property = Helper.GetExpressionProperty(propertyExpression.Body);
            if (property == null)
            {
                throw Helper.Error("Invalid property expression {0}", propertyExpression.Body);
            }
            return new ExecuteReadTo<TViewModel>(this, property);
        }

        internal void AddProperty(string outputField, PropertyInfo property)
        {
            if (_properties.ContainsKey(outputField))
            {
                throw Helper.Error("Output Field {0} is already defined in view model output map for procedure {1}",
                                   outputField,
                                   Procedure.Declaration);
            }
            foreach (var outputProperty in _properties)
            {
                if (outputProperty.Value.Name == property.Name)
                {
                    throw Helper.Error("Property {0} is already defined in view model output map for procedure {1}",
                                       property.Name,
                                       Procedure.Declaration);
                }
            }
            _properties.Add(outputField, property);
        }

        internal object CreateObject(Type modelType, IDataRecord record)
        {
            var model = Activator.CreateInstance(modelType);
            foreach (var property in _properties)
            {
                var recordValue = record[property.Key];
                if (recordValue == null)
                    continue;

                var propertyValue = Helper.ConvertType(recordValue, property.Value.PropertyType);
                property.Value.SetValue(model, propertyValue, null);
            }
            return model;
        }
    }
}
